
*************************************************************************
** FIRST GET IJ GLOBAL EQUITY RECORDS
*************************************************************************
//get borrowers mapped to internal ID
use ../Raw/IJGlobal/transaction_borrower_records, clear
rename Borrower_CompanyName Borrower_CompanyName_IJG
rename Borrower_ParentCompany Borrower_ParentCompany_IJG
duplicates drop
joinby Borrower_CompanyName_IJG using ../Intermediate/ijglobal_borrowerid_map.dta
keep TransactionId borrower_id 
duplicates drop
merge m:1 borrower_id using ../Intermediate/borrower_file, keepusing(borrower_name) keep(3) nogen
tempfile ij_borrower_map
save `ij_borrower_map'

// Get IJ Global Equity Records
use ../Raw/psedo/IJGlobal/equity_records_tranche_investor_level, clear

// CUT DOWN TO GCEL BORROWERS SAMPLE
joinby TransactionId using `ij_borrower_map', unmatched(both) 
egen has_merge = max(_merge==3), by(borrower_id)
unique borrower_id if has_merge == 0 
keep if _merge == 3 
drop _merge has_merge
gen year = yofd(transaction_date)
egen companyid = group(CompanyName)
gcollapse (sum) equity_amt_ijg = SponsorEquityUSDm (nunique) equity_n_trans_ijg = TransactionId equity_n_tranche_ijg = TrancheId equity_n_investors_ijg = companyid, by(borrower_id year)

tempfile ijgequity 
save `ijgequity'


*************************************************************************
** FIRST PUBLIC EQUITY ISSUANCES FROM SDC PLATINUM
*************************************************************************
use ../Raw/psedo/clean_equity_issued, clear

joinby companyid using ../Raw/SDC/sdc_gcel_company_link

//limit to GCEL Sample
keep if ~missing(GCELcompanyID)

 //clean security type
tab Type_of_Security, sort

gen security_type_clean = "Equity" if inlist(Type_of_Security,"Class H Ord Shs","Ordinary Shares","Class A Shares","Ord/Common Shs.", "Class A Ord Shs","Common Shares")
replace security_type_clean = "Units" if Type_of_Security == "Units"
replace security_type_clean = "Debt" if regexm(Type_of_Security,"Bonds") | regexm(Type_of_Security,"Bds") | regexm(Type_of_Security,"Bd") | regexm(Type_of_Security,"Nts")
replace security_type_clean = "Other" if missing(security_type_clean)

gen equity_amt_public = principal_amt
gen equity_n_public = 1

gen year = yofd(Issue_Date)

gcollapse (sum) equity_*, by(GCELParentID year) labelformat(#sourcelabel#)

//merge in borrower_id
rename GCELParentID ParentID_GCEL 
merge m:1 ParentID_GCEL using ../Intermediate/borrower_file, keepusing(borrower_id) nogen
order borrower_id
drop ParentID_GCEL
tempfile sdcequity 
save `sdcequity'

*************************************************************************
** COMBINE IJG AND SDC EQUITY
*************************************************************************
use `sdcequity', clear

//fill out panel 
ds borrower_id year, not
local fillvars `r(varlist)'
gen orig_equity = 1 if ~missing(year)
replace year = 2009 if missing(year)
tsset borrower_id year
tsfill, full 
foreach var of var `fillvars' {
	replace `var' = 0 if orig_equity == .
}
replace orig_equity = 0 if missing(orig_equity)
label var orig_equity "has equity issue in this year"
compress

//merge in ijg equity 
merge 1:1 borrower_id year using `ijgequity', keep(1 3)
foreach var of var *_ijg {
	replace `var' = 0 if _merge == 1
}
gen orig_equity_ijg = _merge == 3
rename orig_equity orig_equity_public
gen orig_equity = orig_equity_ijg | orig_equity_public
drop _merge

egen equity_amt_all = rowtotal(equity_amt_public equity_amt_ijg)
egen equity_n_all = rowtotal(equity_n_public equity_n_investors_ijg)

save ../Intermediate/equity_issuance_panel, replace
